# -*- coding: utf-8 -*-
# @Time : 2024/1/31 13:43
# @Author : ZH
# @File : 36.py
# @Software: PyCharm

from typing import List


class Solution:

    def get_box_index(self, i, j):
        box = [
            {'r': [0, 2], 'c': [0, 2]}, {'r': [0, 2], 'c': [3, 5]}, {'r': [0, 2], 'c': [6, 8]},
            {'r': [3, 5], 'c': [0, 2]}, {'r': [3, 5], 'c': [3, 5]}, {'r': [3, 5], 'c': [6, 8]},
            {'r': [6, 8], 'c': [0, 2]}, {'r': [6, 8], 'c': [3, 5]}, {'r': [6, 8], 'c': [6, 8]}
        ]
        for index, value in enumerate(box):
            if value.get('r')[0] <= i <= value.get('r')[1] and value.get('c')[0] <= j <= value.get('c')[1]:
                return index

    def isValidSudoku(self, board: List[List[str]]) -> bool:
        r = [{} for i in range(9)]  # 行记录
        c = [{} for i in range(9)]  # 列记录
        b = [{} for i in range(9)]  # 方块记录 9x9被分为9个3x3的数组

        for i in range(9):
            for j in range(9):
                b_index = self.get_box_index(i, j)
                if board[i][j] != '.':
                    r[i][board[i][j]] = r[i].get(board[i][j], 0) + 1
                    c[j][board[i][j]] = c[j].get(board[i][j], 0) + 1
                    b[b_index][board[i][j]] = b[b_index].get(board[i][j], 0) + 1

                    if r[i][board[i][j]] > 1 or c[j][board[i][j]] > 1 or b[b_index][board[i][j]] > 1:
                        return False
        return True
